home *** CD-ROM | disk | FTP | other *** search
- /*
- * Dialog Module, created by Resorcerer
- */
-
- #if 0
- #include <Desk.h>
- #include <DiskInit.h>
- #include <Dialogs.h>
- #include <Files.h>
- #include <Fonts.h>
- #include <Lists.h>
- #include <Menus.h>
- #include <Resources.h>
- #include <Memory.h>
- #include <OSUtils.h>
- #include <OSEvents.h>
- #include <Packages.h>
- #include <Scrap.h>
- #include <Script.h>
- #include <SegLoad.h>
- #include <stdio.h>
- #include <ToolUtils.h>
- #include <Values.h>
- #endif
-
- #include "AsyncPrefsDialog.h"
- #include "ResorcererDialogLib.h"
-
- long gNumIterations = 10;
- Str63 gNameString = "\pResults";
- Boolean gRunRandom = true;
-
- /* Symbolic Dialog Item Numbers */
-
- static enum {
- BUT1_OK = 1,
- BUT2_Cancel,
- dIterText,
- dNameText,
- dRandomBut,
- STXT6_Run,
- STXT7_times,
- STXT8_Put,
- LASTITEM
- };
-
- #define OK_ITEM BUT1_OK
- #define CANCEL_ITEM BUT2_Cancel
-
- /* Useful constants */
-
- /* Prototypes */
-
-
- pascal Boolean MyFilter(DialogPtr dlog, EventRecord *evt, short *itemHit);
- Boolean CheckUserItems(Point where, short *itemHit);
- int AnyBadValues(DialogPtr dlog);
-
- extern Boolean gModalDialogActive;
- extern DialogPtr gModalDialog;
-
- static Point where;
- static int modifiers;
-
- /*
- * For non-modal dialogs, after a mouse down event in dialog window, w, call this
- * to entertain the click.
- */
-
- #define kNameStrings 512
- #define kPrefsID 128
- struct PrefsResource {
- short numIterations;
- short randomize;
- Str63 fileName;
- };
-
- typedef struct PrefsResource PrefsResource, **PrefsHandle;
-
- static OSErr GetPrefsFile(FSSpec *prefsFileSpec);
-
- static OSErr GetPrefsFile(FSSpec *prefsFileSpec)
- {
- OSErr err;
- short vRefNum;
- long dirID;
- Str255 prefsFileName;
-
- err = FindFolder(kOnSystemDisk, kPreferencesFolderType, kDontCreateFolder, &vRefNum, &dirID);
- if (err == noErr) {
- GetIndString(prefsFileName, kNameStrings, 1);
- err = FSMakeFSSpec(vRefNum, dirID, prefsFileName, prefsFileSpec);
- }
- return err;
- }
-
- void ReadAsyncPrefs()
- {
- OSErr err;
- FSSpec prefsFileSpec;
- short refNum = -1;
- PrefsHandle prefs;
-
- // Try to get the preferences information from a prefs file. If none is
- // found, get the default preferences from the application's resource fork
- err = GetPrefsFile(&prefsFileSpec);
- if (err == noErr) {
- refNum = FSpOpenResFile(&prefsFileSpec, fsRdPerm);
- }
-
- // Read in the startup preferences
- prefs = (PrefsHandle)GetResource('APRF', 128);
- if (prefs == nil) {
- // Set up default values 'cause something really went wrong
- gNumIterations = 10;
- gRunRandom = true;
- BlockMove((Ptr)"\pResults", gNameString, 8);
- } else {
- gNumIterations = (**prefs).numIterations;
- gRunRandom = (**prefs).randomize;
- BlockMove((Ptr)&((**prefs).fileName), (Ptr)gNameString, sizeof(Str63));
- ReleaseResource((Handle)prefs);
- }
-
- if (refNum != -1)
- CloseResFile(refNum);
- }
-
- void WriteAsyncPrefs()
- {
- OSErr err;
- FSSpec prefsFileSpec;
- short homeRefNum,
- refNum = -1;
- PrefsHandle prefs;
-
- // Try to get the preferences information from a prefs file. If none is
- // found, get the default preferences from the application's resource fork
- err = GetPrefsFile(&prefsFileSpec);
- if (err == fnfErr) {
- DebugStr("\pCReating prefs file");
- FSpCreateResFile(&prefsFileSpec, 'RDC1', 'PREF', smRoman);
- err = ResError();
- if (err != noErr) goto done;
- }
- refNum = FSpOpenResFile(&prefsFileSpec, fsRdWrPerm);
- if (refNum == -1) goto done;
-
- // Read in the current preferences resource
- prefs = (PrefsHandle)GetResource('APRF', 128);
- if (prefs == nil) goto done; // We can't find the prefs resource in our preferences
- // file or in the application's resource fork. This is bad.
-
- // If this resource isn't in the prefs file, add it
- homeRefNum = HomeResFile((Handle)prefs);
- if (homeRefNum == -1) goto done; // Not a handle to a resource!
-
- if (homeRefNum != refNum) {
- // The resource came from our application, so copy it into the prefs file
- DetachResource((Handle)prefs);
- UseResFile(refNum);
- AddResource((Handle)prefs, 'APRF', 128, "\pPreferences");
- ChangedResource((Handle)prefs);
- UpdateResFile(refNum);
- }
-
- // Update the contents of the prefs resource and mark it as changed
- (**prefs).numIterations = gNumIterations;
- (**prefs).randomize = gRunRandom;
- BlockMove((Ptr)gNameString, (Ptr)&((**prefs).fileName), sizeof(Str63));
- ChangedResource((Handle)prefs);
-
- done:
- if (refNum != -1)
- CloseResFile(refNum);
- }
-
- void DoDialogContent(DialogPtr w, EventRecord *evt)
- {
- short itemHit;
-
- SetPort(w); /* If it's not already */
-
- modifiers = evt->modifiers; /* Stuff our local globals */
- where = evt->where;
- GlobalToLocal(&where);
-
- if (CheckUserItems(where,&itemHit) || DialogSelect(evt,&w,&itemHit))
- if (!DoDialogItem(w,itemHit))
- CloseThisDialog(w);
- }
-
- /*
- * Mouse down event:
- * Check if it's in some user item, and convert to itemHit if appropriate.
- */
-
- static Boolean CheckUserItems(Point where, short *itemHit)
- {
- return(FALSE);
- }
-
- /*
- * Redraw the contents of this dialog due to update event.
- * If you have not installed UserItem draw routines, you should redraw
- * them explicitly here; otherwise, UpdtDialog() will call your routines.
- */
-
- void DoDialogUpdate(DialogPtr dlog)
- {
- GrafPtr oldPort;
-
- GetPort(&oldPort); SetPort(dlog);
- BeginUpdate(dlog);
-
- UpdtDialog(dlog,dlog->visRgn);
- FrameDefault(dlog,BUT1_OK,TRUE);
-
- EndUpdate(dlog);
- SetPort(oldPort);
- }
-
- /*
- * Activate event: Activate or deactivate this dialog and any items in it
- */
-
- void DoDialogActivate(DialogPtr dlog, int activ)
- {
- SetPort(dlog);
- }
-
- /*
- * Build this dialog's window on desktop, and install initial item values.
- * Return the dlog opened, or NIL if error (no resource, no memory).
- */
-
- DialogPtr OpenThisDialog()
- {
- short type; Handle hndl; Rect box; GrafPtr oldPort;
- DialogPtr dlog; unsigned char *p,str[256];
-
- GetPort(&oldPort);
- dlog = GetNewDialog(thisDialogID,NIL,FRONT_WINDOW);
- if (dlog == NIL) { SysBeep(1); return(NIL); } /* Poor man's error message */
-
- CenterWindow(dlog,0);
- SetPort(dlog);
-
- /* Fill in dialog's values here */
-
- PutDlgLong(dlog, dIterText,gNumIterations, TRUE);
- PutDlgString(dlog, dNameText,(void*)gNameString, FALSE);
-
- PutDlgChkRadio(dlog, dRandomBut, gRunRandom);
-
- ShowWindow(dlog);
- return(dlog);
-
- }
-
- /*
- * Clean up any allocated stuff, and return dialog to primordial mists
- */
-
- void CloseThisDialog(DialogPtr dlog)
- {
- DisposDialog(dlog); /* Call CloseDialog if you provide storage to GetNewDialog */
- gModalDialogActive = false;
- gModalDialog = NULL;
- }
-
- /*
- * Deal with user clicking on an item in this dialog, either modal or non-modal.
- * The local point is in where; modifiers in modifiers.
- * Returns whether or not the dialog should be closed (keepGoing).
- */
-
- int DoDialogItem(DialogPtr dlog, short itemHit)
- {
- short type,okay=FALSE,keepGoing=TRUE,val;
- Handle hndl; Rect box; Point pt;
- unsigned char *p,str[256];
-
- if (itemHit<1 || itemHit>=LASTITEM)
- return(keepGoing); /* Only legal items, please */
-
- GetDItem(dlog,itemHit,&type,&hndl,&box);
- switch(type) {
- case ctrlItem+btnCtrl:
- switch(itemHit) {
- case BUT1_OK:
- keepGoing = FALSE; okay = TRUE;
- break;
- case BUT2_Cancel:
- keepGoing = FALSE;
- break;
- }
- break;
- case ctrlItem+chkCtrl:
- SetCtlValue((ControlHandle)hndl,val = !GetCtlValue((ControlHandle)hndl));
- switch(itemHit) {
- case dRandomBut:
- break;
- }
- break;
- case ctrlItem+radCtrl:
- break;
- case ctrlItem+resCtrl:
- break;
- case statText:
- switch(itemHit) {
- case STXT6_Run: /* NOT Enabled */
- break;
- case STXT7_times: /* NOT Enabled */
- break;
- case STXT8_Put: /* NOT Enabled */
- break;
- }
- break;
- case editText:
- switch(itemHit) {
- case dIterText:
- break;
- case dNameText:
- break;
- }
- break;
- case iconItem:
- break;
- case picItem:
- break;
- case userItem:
- break;
- }
-
- if (okay) {
- keepGoing = AnyBadValues(dlog);
- if (!keepGoing) {
- // Extract final values
- GetDlgLong(dlog,dIterText,&gNumIterations);
- GetDlgString(dlog,dNameText,(char*)gNameString);
- gRunRandom = (Boolean)GetDlgChkRadio(dlog, dRandomBut);
- }
- }
- return(keepGoing);
- }
-
- /*
- * Pull values out of dialog items and deliver TRUE if any of them are
- * illegal or inconsistent; otherwise deliver FALSE. If any values are bad,
- * you should inform your user about the problem here before delivering TRUE.
- * If any items are missing values, this is the place to assign any defaults.
- */
-
- int AnyBadValues(DialogPtr dlog)
- {
- char str[256]; long val;
-
- if (!GetDlgLong(dlog,dIterText,&val)) {
- PutDlgLong(dlog,dIterText,gNumIterations, TRUE);
- return true;
- }
-
- GetDlgString(dlog,dNameText,str);
- if (true) {
- Boolean changed = false;
- int count;
-
- if (*str)
- // Remove any path separators
- for (count = *str; count >= 1; count--) {
- if (str[count] == ':') {
- str[count] = '-';
- changed = true;
- }
- }
-
- if (changed) {
- PutDlgString(dlog,dNameText,str, TRUE);
- return true;
- }
-
- if ((*str == (char)0) || (*str > (char)63)) {
- PutDlgString(dlog,dNameText,(char*)gNameString, TRUE);
- return true;
- }
- }
-
- return(FALSE);
- }
-
-
-